package cn.git.shiro.controller;

import cn.git.shiro.constant.ShiroJWTConstant;
import cn.git.shiro.entity.User;
import cn.git.shiro.result.Result;
import cn.git.shiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 登录controller
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-03-27
 */
@RestController
@RequestMapping("/shiro")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/login")
    public Result<String> login(@RequestParam String username, @RequestParam String password) {
        // 从数据库中查找用户的信息，信息正确生成token
        return userService.login(username, password);
    }

    /**
     * 获取用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public Result<User> detail(@PathVariable(value = "id") String id) {
        return Result.ok(userService.getUserById(id));
    }

    /**
     * 登出
     *
     * @param jwtToken
     * @return
     */
    @GetMapping("/logout")
    public Result<String> logout(@RequestHeader(value = ShiroJWTConstant.JWT_TOKEN_FLAG) String jwtToken) {
        // 直接redis登出即可，jwtToken过期自动登出
        userService.logout(jwtToken);
        return Result.ok("登出成功");
    }

}
